{include file="public/head"/}
<div id="main-page">
    <div class="wap">
        <div id="cardbox" class="data-box">
          {foreach $list as $item}<?php $card=json_decode($item['card'], true)?>
          <div class="cardbox">
            <div class="cardleft">
              <p class="cardname textone" id="cardname{$item['id']}">{$card['name']}</p>
              {if $card['starttime']}
                {if $card['endtime']}
                <p class="cardterm textone" id="cardterm{$item['id']}">{$card['starttime']} ~ {$card['endtime']}</p>
                {elseif $card['term']==0}
                <p class="cardterm textone" id="cardterm{$item['id']}">{$card['starttime']} ~ 永久}</p>
                {else}
                <p class="cardterm textone" id="cardterm{$item['id']}">{$card['starttime']} ~ {:date('Y-m-d H:i:s', $item['createtime']+$card['term']*86400)}</p>
                {/if}
              {elseif $card['endtime']}
                <p class="cardterm textone" id="cardterm{$item['id']}">有效期至：{$card['endtime']}</p>
              {elseif $card['term']==0}
                <p class="cardterm textone" id="cardterm{$item['id']}">永久有效</p>
              {else}
                <p class="cardterm textone" id="cardterm{$item['id']}">有效期至：{:date('Y-m-d H:i:s', $item['createtime']+$card['term']*86400)}</p>
              {/if}
              <p class="cardcode textone" id="cardcode{$item['id']}">核销码：{$item['code']}</p>
            </div>
            <div class="cardright" data-id="{$item['id']}" id="cardright{$item['id']}">
              {if $item['status']==0}
              <span>未使用</span>
              {else}
              <span class="fc-green">已使用</span>
              {/if}
            </div>
            <div class="cirt"></div>
            <div class="cirb"></div>
          </div><input type="hidden" id="desc{$item['id']}" value="{$card['desc']}">
          {/foreach}
        </div>
        <div class="card">
          <div class="cardclose">×</div>
          <div class="cardbox2">
            <div class="cardleft2">
              <p class="cardname2 textone"></p>
              <p class="cardterm2 textone"></p>
              <p class="cardcode2 textone"></p>
            </div>
            <div class="cardright2"></div>
            <div class="cirt2"></div>
            <div class="cirb2"></div>
          </div>
          <p class="tac" id="desc"></p>
          <p class="tac">
            <img src="" id="qrcode" style="max-width:100%;">
          </p>
          {if $sale['card']['pwd']}
          <div class="row" id="cardpwd">
            <div class="col-8">
              <input type="text" name="pwd" id="pwd" placeholder="请找店员要核销密码" style="width:100%;height:0.7rem;line-height:0.7rem;border:1px solid #E0E0E0;border-radius:0.1rem;padding:0 0.1rem;box-shadow: inset 0 0.04rem 0.02rem rgba(0,0,0,0.1);">
            </div>
            <div class="col-4">
              <button type="button" class="btn btn-block btn-blue" id="pwdverifyer" data-id="0">核销</button>
            </div>
            <div style="clear:both;padding:0.15rem;"></div>
          </div>
          {/if}
          <div class="row pt-1">
            {if isset($sale['poi_share'])}
            <div class="{if isset($sale['poi_share']) && isset($sale['real_share'])}col-6{else}col-12{/if}">
              <a href="{$sale['poi_share']}" target="_blank" class="btn btn-block btn-red" style="height:0.7rem;line-height:0.7rem;">商户位置</a>
            </div>
            {/if}
            {if isset($sale['real_share'])}
            <div class="{if isset($sale['poi_share']) && isset($sale['real_share'])}col-6{else}col-12{/if}">
              <a href="{$sale['real_share']}" target="_blank" class="btn btn-block btn-red" style="height:0.7rem;line-height:0.7rem;">前往商户</a>
            </div>
            {/if}
          </div>
        </div>
        <div class="card-overlay"></div>
    </div>
    <script>
      $(function(){
        var page = {$page};
        var limit = {$limit};
        var count = {$count};
        var totalheight = $(document).height()-50;//定义一个总的高度变量
        
        $(window).scroll(function () {
            if(count<limit*page){
                return false;
            }
            totalheight = parseFloat($(window).height()) + parseFloat($(window).scrollTop());//浏览器的高度加上滚动条的高度 
            if ($(document).height() <= totalheight){ //当文档的高度小于或者等于总的高度的时候，开始动态加载数据
                page++;
                $.get("{$_W['baseurl']}/web/card?sale_id={:input('sale_id')}&page="+page+'&limit='+limit, function(res){
                    var data = $(res).find('.data-box').html();
                    $('.data-box').append(data);
                });
            }
        });
        $('.cardright').on('click', function(){
          if($(this).text().trim() == '已使用'){
            return false;
          }
          var id = $(this).data('id');
          $('#pwdverifyer').data('id', id);
          $('.cardname2').html($('#cardname'+id).html());
          $('.cardterm2').html($('#cardterm'+id).html());
          $('.cardcode2').html($('#cardcode'+id).html());
          $('.cardright2').html($('#cardright'+id).html());
          $('#desc').html($('#desc'+id).val());
          $('#qrcode').attr('src', "{$_W['baseurl']}/web/card/qrcode?id="+id)
          $('.card-overlay').fadeIn(50);
          $('.card').fadeIn(100);
        });

        $('.cardclose,.card-overlay').on('click', function(){
          $('.card-overlay').fadeOut(50);
          $('.card').fadeOut(100);
        });

        $('#pwdverifyer').on('click', function(){
          var id  = $(this).data('id');
          var pwd = $('#pwd').val();
          $.get("{$_W['baseurl']}/web/card/pwdverifyer?id="+id+"&pwd="+pwd, function(res){
            if(res.code == 0){
              $('#cardright'+id).html('<span class="fc-green">已使用</span>');
              $('.card-overlay').fadeOut(50);
              $('.card').fadeOut(100);
            }
            dialog(res.msg);
          });
        });
      });
    </script>
</div>
{include file="public/footer"/}